import 'package:oramemo/entity/create_order_entity.dart';
import 'package:oramemo/entity/membr_level_entity.dart';

import '../network/dio_client.dart';
import '../config/api_config.dart';
import '../models/api_response.dart';
import '../models/transaction.dart';

class TransactionRepository {
  final DioClient _client = DioClient();

  Future<ApiResponse<MembrLevelEntity>> getMemberLevels() async {
    final response = await _client.request(
      path: ApiConfig.memberLevels,
      method: 'GET',
    );
    return ApiResponse.fromJson(
      response.data,
      (data) =>  MembrLevelEntity.fromJson(data),
    );
  }

  Future<ApiResponse<List<Transaction>>> getTransactions({
    required int page,
    required int size,
    String? type,
    String? status,
  }) async {
    final response = await _client.request(
      path: ApiConfig.transactions,
      method: 'GET',
      queryParameters: {
        'page': page,
        'size': size,
        if (type != null) 'type': type,
        if (status != null) 'status': status,
      },
    );
    return ApiResponse.fromJson(
      response.data,
      (data) => (data as List).map((e) => Transaction.fromJson(e)).toList(),
    );
  }

  Future<ApiResponse<CreateOrderEntity>> createOrder({
    required String memberLevelId,
    String? couponCode,
  }) async {
    final response = await _client.request(
      path: ApiConfig.createOrder,
      method: 'POST',
      data: {
        'memberLevelId': memberLevelId,
        if (couponCode != null) 'couponCode': couponCode,
      },
    );
    return ApiResponse.fromJson(response.data, (data) => CreateOrderEntity.fromJson(data));
  }
}